package com.mingqijia.gassafety.authority.security.oauth;

import com.gap.basic.base.CommonResponse;
import com.gap.cache.redis.RedisCache;
import com.mingqijia.gassafety.authority.security.valueobject.ManagerVO;
import com.mingqijia.gassafety.authority.security.user.SecurityUserLoginHolder;
import com.mingqijia.gassafety.shared.utils.ApplicationUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.logout.LogoutSuccessHandler;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 自定义退出处理类 返回成功
 * @author Damon S.
 * @version v1.0.1
 * @date 2020年08月04日 22:32
 */
@Configuration
@RequiredArgsConstructor
public class LogoutSuccessHandlerImpl implements LogoutSuccessHandler {

    private final RedisCache cacheManager;
    private final SecurityUserLoginHolder holder;

    @Override
    public void onLogoutSuccess(HttpServletRequest request,
                                HttpServletResponse response,
                                Authentication auth) throws IOException, ServletException {
        ManagerVO backend = (ManagerVO) auth.getPrincipal();
        holder.whenManagerLogout(backend);

        // 设置返回信息
        ApplicationUtils.writeOutJson(response, CommonResponse.success(null));
    }
}
